<template>
  <div class="main">
    <div class="body">
      <div class="rowInput">
        <div>
          <el-row :gutter="20">
            <el-col :span="6"
              ><div class="grid-content bg-purple">
                <span>标签名称</span
                ><el-input placeholder="请输入内容"></el-input></div
            ></el-col>

            <el-col :span="6"
              ><div class="grid-content bg-purple">
                <span>企业简称</span
                ><el-input placeholder="请输入内容"></el-input></div
            ></el-col>
            <div class="btn">
              <el-button size="small" @click="reset">清空</el-button>
              <el-button type="primary" size="small" @click="search"
                >搜索</el-button
              >
              <div class="rigthbtn">
                <el-button
                  type="primary"
                  class="add"
                  icon="el-icon-edit"
                  size="small"
                  @click="changeData('add')"
                  >新增技巧</el-button
                >
              </div>
            </div>
          </el-row>
        </div>
      </div>
      <div class="record">
        <span>共 {{ counts }} 条记录</span>
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="序号" width="130">
          </el-table-column>
          <el-table-column prop="title" label="文章标题" width="170">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.title }}</span>
              <a href="#">
                <i
                  @click="video(scope.row)"
                  v-if="scope.row.videoURL"
                  class="el-icon-film"
                ></i
              ></a>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数" width="177">
          </el-table-column>
          <el-table-column prop="username" label="录入人" width="177">
          </el-table-column>
          <el-table-column prop="createTime" label="录入时间" width="177">
          </el-table-column>
          <el-table-column prop="state" label="状态" width="177">
          </el-table-column>
          <el-table-column prop="avatar" label="操作">
            <template #default="{ row }">
              <span>
                <el-link type="primary" @click="lookData(row)"
                  >预览</el-link
                ></span
              >
              <span>
                <el-link type="primary" @click="changeState(row)">{{
                  row.state === '启用' ? '禁用' : '启用'
                }}</el-link></span
              >
              <span>
                <el-link
                  type="primary"
                  @click="changeData('change', row)"
                  :disabled="row.state === '禁用'"
                  >修改</el-link
                ></span
              >
              <span>
                <el-link
                  type="primary"
                  @click="delList(row)"
                  :disabled="row.state === '禁用'"
                  >删除</el-link
                ></span
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination
          background
          layout="prev,pager,next,sizes,jumper"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="data.pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
      <!-- 新增用户弹窗 -->
      <div>
        <el-dialog title="新增文章" :visible.sync="addDialog" width="50%">
          <span slot="footer" class="dialog-footer">
            <div class="form">
              <el-form
                :rules="rules"
                :model="form"
                ref="form"
                label-width="90px"
              >
                <el-form-item label="文章标题：" prop="title">
                  <el-input placeholder="请输入文章标题" v-model="form.title" />
                </el-form-item>
                <el-form-item label="文章内容：">
                  <quillEditor v-model="form.articleBody"></quillEditor>
                </el-form-item>
                <el-form-item label="视频地址">
                  <el-input
                    placeholder="请输入视频地址"
                    v-model="form.videoURL"
                  />
                </el-form-item>
              </el-form>
              <div class="button">
                <el-button @click="addDialog = false">取 消</el-button>
                <el-button type="primary" @click="addList">确 定</el-button>
              </div>
            </div>
          </span>
        </el-dialog>
      </div>
      <!-- 预览弹窗 -->
      <div>
        <el-dialog title="预览" :visible.sync="addLookDialog" width="50%">
          <span slot="footer" class="dialog-footer">
            <div class="form">
              <div>
                <h2>{{ lookData1.title }}</h2>
              </div>
              <div class="text">
                <div>{{ lookData1.createTime }}</div>
                &nbsp;&nbsp;&nbsp;
                <div>{{ lookData1.username }}</div>
                &nbsp;&nbsp;&nbsp;
                <div><i class="el-icon-view"></i> {{ lookData1.visits }}</div>
              </div>
              <div v-html="lookData1.articleBody" class="aabb"></div>
            </div>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, add, update } from '@/api/hmmm/articles.js'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

export default {
  components: {
    quillEditor
  },
  data () {
    return {
      tableData: [],
      list: [],
      data: {
        page: 1,
        pagesize: 10
      },
      input: '',
      total: 0,
      counts: '',
      addDialog: false,
      addLookDialog: false,
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }]
      },
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      lookData1: {}
    }
  },

  async created () {
    this.init()
  },

  methods: {
    // 判断状态是启用还是禁用
    changeState (row) {
      this.tableData.forEach(item => {
        if (item.id === row.id) {
          if (row.state === '启用') {
            item.state = '禁用'
          } else {
            item.state = '启用'
          }
        }
      })
    },

    // 视频
    video (row) {
      console.log(row)
    },
    async init () {
      const { data } = await list(this.data)
      console.log(data)
      this.tableData = data.items
      this.tableData.forEach(item => {
        item.state ? (item.state = '启用') : (item.state = '禁用')
      })
      this.list = data.items
      this.total = data.pages * 10
      this.counts = data.counts
    },
    // 添加功能
    async addList () {
      this.addDialog = false
      if (this.form.creatorID) {
        const data = await update(this.form)
        console.log(data)
        this.init()
      } else {
        await add(this.form)
        this.init()
      }
    },
    // 修改表单
    changeData (type, row) {
      this.addDialog = true
      if (type === 'add') {
        this.form = {}
      }
      if (type === 'change') {
        console.log(row)
        this.form = { ...row }
      }
    },
    // 重置表单
    reset () {
      this.input = ''
    },
    // 删除功能
    async delList (row) {
      const data = await remove(row)
      console.log(data)
      this.init()
    },
    // 搜索功能
    search () {
      this.tableData = []
      this.list.forEach(item => {
        if (item.username.includes(this.input)) {
          this.tableData.push(item)
        }
      })
      console.log(this.tableData)
    },
    handleSizeChange (val) {
      console.log(val)
      this.data.pagesize = val
      this.init()
    },
    handleCurrentChange (val) {
      console.log(val)
      this.data.page = val
      this.init()
    },
    lookData (row) {
      this.lookData1 = row
      this.addLookDialog = true
    }
  }
}
</script>

<style scoped lang="less">
::v-deep .el-dialog__footer {
  text-align: left;
}
::v-deep .ql-container.ql-snow {
  height: 200px;
}
.el-link.el-link--primary.is-underline {
  margin-right: 5px;
}
.el-link.el-link--primary.is-disabled {
  margin-right: 5px;
}
.el-link.el-link--primary.is-disabled {
  color: #909399;
}
::v-deep [data-v-0fe85c23] .el-input--suffix .el-input__inner {
  padding: 0;
}
::v-deep .el-select.el-select--medium {
  width: 144px;
  margin-left: 10px;
}
::v-deep .el-form-item__label {
  width: 92px !important;
}
::v-deep .el-form-item__content {
  margin-left: 92px !important;
}
.rowInput {
  margin-bottom: 15px;
}
.grid-content.bg-purple {
  display: flex;
  span {
    width: 80px;
    line-height: 30px;
    font-size: 14px;
    color: #606266;
  }
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.button {
  margin-top: 30px;
  text-align: center;
}
::v-deep .el-input--suffix .el-input__inner {
  padding-right: 86px;
}
::v-deep .el-dialog__body {
  display: none !important;
}
.form {
  // width: 400px;
  // margin-top: 30px;
  margin: 30px auto;
  span {
    font-size: 12px;
  }
  .text {
    display: flex;
  }
}
::v-deep .el-table th.el-table__cell {
  background-color: #fafafa;
}
::v-deep td {
  text-align: center !important;
}

::v-deep th {
  text-align: center !important;
  background-color: #fafafa;
  border-bottom: 1px solid #ebeef5;
}
::v-deep .el-input--medium .el-input__inner {
  height: 32px;
}
::v-deep .add.el-button.el-button--primary.el-button--small {
  background-color: #67c23a;
  border-color: #67c23a;
}
.headinp {
  flex: 1;
  display: flex;
}
.main {
  padding: 20px;
  .body {
    // height: 100px;
    background-color: #fff;
    padding: 20px;
    .inp {
      // background-color: #ad1b1b;
      height: 40px;
      display: flex;
      .input {
        width: 280px;
        height: 32px;
        display: flex;
        span {
          width: 80px;
          line-height: 30px;
          font-size: 14px;
          color: #606266;
        }
      }
      .btn {
        margin-left: 15px;
      }
    }
    .record {
      height: 32px;
      background-color: #f4f4f5;
      margin: 10px 0;
      line-height: 32px;
      font-size: 13px;
      color: #909399;
    }
    .pagination {
      margin-top: 20px;
      text-align: right;
    }
  }
}

.inb1.el-input.el-input--medium {
  width: 200px !important;
}
.main .body .inp .input span[data-v-0fe85c23] {
  width: 62px;
}
.inb1.el-input.el-input--medium[data-v-0fe85c23] {
  width: 215px !important;
}
.el-select.select1.el-select--medium {
  width: 215px;
}
.rigthbtn {
  display: inline-block;
  float: right;
}
i {
  font-size: 16px;
  color: #4579e1;
}
.aabb {
  margin-top: 20px;
  height: 80px;
  background-color: #f5f5f5;
  border-top: 1px dashed #606266;
  line-height: 40px;
}
</style>
